unity play audio from particle system

111

unity play audio from particle system -

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(ParticleSystem))]
public class CollidingParticles : MonoBehaviour {

    public AudioClip collisionSFX;

    ParticleSystem partSystem;
    ParticleCollisionEvent[] collisionEvents;

    void Awake () {

        partSystem = GetComponent<ParticleSystem>();
        collisionEvents = new ParticleCollisionEvent[16];
    }

    void OnParticleCollision (GameObject other) {

        int safeLength = partSystem.GetSafeCollisionEventSize();
        if (collisionEvents.Length < safeLength)
            collisionEvents = new ParticleCollisionEvent[safeLength];

        int totalCollisions = partSystem.GetCollisionEvents(other, collisionEvents);
        for (int i = 0; i < totalCollisions; i++)
            AudioSource.PlayClipAtPoint(collisionSFX, collisionEvents[i].intersection);

        print (totalCollisions);
    }
}

Comments

Submit
0 Comments